package com.free.web;

import com.free.dao.TestFreeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2020/12/11  16:23
 * 描述:
 */
@Controller
public class TestFreeController {


    @Autowired
    private TestFreeDao testFreeDao;

    @RequestMapping("list")
    public String ListPage(HttpServletRequest request, HttpServletResponse response){

        Map<String,Object> map=new HashMap<String,Object>();

        List<Map<String, Object>> list = testFreeDao.list(map);

        for (Map<String,Object> mm:list ) {

            mm.put("id",mm.get("id").toString());

        }

        request.setAttribute("dataList",list);

        return "list";
    }


    @RequestMapping("/del")
    @ResponseBody
    public String del(@RequestParam String id){

        System.out.println("================id====");

        int i = testFreeDao.delOne(id);

        if(i==1){
           return "OK";
        }else{
            return "FAIL";
        }

    }



    /**
     * 作者: LCG
     * 日期: 2020/12/11  16:49
     * 描述: 添加一个
     * @Param []
     * @Return int
     */
    @RequestMapping("add")
    @ResponseBody
    public int addOne(@RequestParam("userName") String userName){

        if(userName!=null&&userName.length()>0){

            Map<String,Object> map = new HashMap<String, Object>();
            map.put("userName",userName);
            map.put("id",System.currentTimeMillis());

            testFreeDao.insertOne(map);

        }

        return 1;
    }


    @RequestMapping("getOne")
    @ResponseBody
    public Map<String,Object> getOne(@RequestParam("id") String id){

        Map<String, Object> one = testFreeDao.getOne(id);

        return one;
    }

}
